package com.ce.pms.occupation.dao.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.occupation.dao.IThingEntrustDao;
import com.ce.pms.occupation.model.ThingEntrustDO;
import com.ce.pms.occupation.vo.ThingEntrustQueryConditionVO;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 物品托管DAO
 * @author 王浩羽
 *
 */
public class ThingEntrustDaoImpl extends PmsBaseDaoImpl<ThingEntrustDO>  implements IThingEntrustDao
{
	public Page queryThingEntrustList(ThingEntrustQueryConditionVO queryCondition)
	{
		StringBuilder hqlBuilder = new StringBuilder();
		List params = new ArrayList();
		hqlBuilder.append("SELECT ");
		hqlBuilder.append(" e.THING_ENTRUST_ID as keyword, ");
		hqlBuilder.append(" b.BUILDING_ID as buildingId, ");
		hqlBuilder.append(" b.NAME as buildingName, ");
		hqlBuilder.append(" e.HOUSE_ID as houseId, ");
		hqlBuilder.append(" h.HOUSE_NUMBER as houseNumber, ");
		hqlBuilder.append(" e.ENTRUST_MAN as entrustMan, ");
		hqlBuilder.append(" e.ENTRUST_ITEM as entrustItem, ");
		hqlBuilder.append(" e.EMPLOYEE_ID as accepterId, ");
		hqlBuilder.append(" em.EMPLOYEE_NAME as accepterName, ");
		hqlBuilder.append(" e.ACCEPT_DATE as acceptDate, ");
		hqlBuilder.append(" e.ENTRUST_REASON as entrustReason, ");
		hqlBuilder.append(" e.ENABLE_STATE as enableState, ");
		hqlBuilder.append(" g.GIVEBACK_MAN as givebackMan, ");
		hqlBuilder.append(" g.GIVEBACK_DATE as givebackDate, ");
		hqlBuilder.append(" g.DESCRIPTION as givebackDescription ");
		hqlBuilder.append(" from OM_THING_ENTRUST e ");
		hqlBuilder.append(" left join OM_THING_GIVEBACK g on e.THING_ENTRUST_ID = g.THING_ENTRUST_ID ");
		hqlBuilder.append(" left join HM_HOUSE h on  h.HOUSE_ID = e.HOUSE_ID ");
		hqlBuilder.append(" left join HM_BUILDING b on  b.BUILDING_ID = h.BUILDING_ID ");
		hqlBuilder.append(" left join HM_EMPLOYEE em on  em.EMPLOYEE_ID = e.EMPLOYEE_ID ");
		
		hqlBuilder.append("where 1=1 ");
		//楼栋ID
		Long buildingDomainId = queryCondition.getBuildingId();
		if(buildingDomainId != null)
		{
			hqlBuilder.append(" AND h.BUILDING_ID = ? ");
			params.add(buildingDomainId);
		}
		//房号
		String houseNumber = queryCondition.getHouseNumber();
		if(StringUtils.hasText(houseNumber))
		{
			hqlBuilder.append(" AND h.HOUSE_NUMBER like ? ");
			params.add("%"+houseNumber+"%");
		}
		//托管人
		String entrustMan = queryCondition.getEntrustMan();
		if(StringUtils.hasText(entrustMan))
		{
			hqlBuilder.append(" AND e.ENTRUST_MAN like ? ");
			params.add("%"+entrustMan+"%");
		}
		//托管物品
		String entrustItem = queryCondition.getEntrustItem();
		if(StringUtils.hasText(entrustItem))
		{
			hqlBuilder.append(" AND e.ENTRUST_ITEM like ? ");
			params.add("%"+entrustItem+"%");
		}
		hqlBuilder.append(" AND e.ENABLE_STATE = '1'  ");//只找有效的
		if(queryCondition.getReturnStatus().equals(ThingEntrustQueryConditionVO.RETURN_STATUS_RETURNED))
		{
			hqlBuilder.append(" AND g.GIVEBACK_MAN is not null ");
		} 
		
		if(queryCondition.getReturnStatus().equals(ThingEntrustQueryConditionVO.RETURN_STATUS_NOT_RETURNED))
		{
			hqlBuilder.append(" AND g.GIVEBACK_MAN is null ");
		}
		Object[] paramsObj = params.toArray();
		queryCondition.checkRule();
		Page page  = this.pagedSqlQueryByPageNo(hqlBuilder.toString(), queryCondition.getPageNo(), queryCondition.getPageSize(), paramsObj);
		List<ThingEntrustQueryConditionVO> list = this.resultSet2QueryConditionVOList(page.getResult());
		page.setResult(list);
		return page;
	}
	
	/**
	 * 功能描述: 将结果集转换为List&lt;ThingEntrustQueryConditionVO&gt; <br>
	 * @param objList
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 15, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<ThingEntrustQueryConditionVO> resultSet2QueryConditionVOList(List<Object[]> objList){
		List<ThingEntrustQueryConditionVO> list = new ArrayList<ThingEntrustQueryConditionVO>();
		if(objList==null || objList.isEmpty())
		{
			return null;
		}
		for(int i = 0; i < objList.size(); i++)
		{
			ThingEntrustQueryConditionVO conditionVO = result2QueryConditionVO(objList.get(i));
			list.add(conditionVO);
		}
		return list;
	}
	
	/**
	 * 功能描述: 查询结果转换为ThingEntrustQueryConditionVO <br>
	 * @param objects
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 15, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private ThingEntrustQueryConditionVO result2QueryConditionVO(Object[] objects){

		if(objects==null)
		{
			return null;
		}
		ThingEntrustQueryConditionVO conditionVO = new ThingEntrustQueryConditionVO();
		//keyword
		conditionVO.setKeyword(objects[0]==null?null:new Long(objects[0].toString()));
		//buildingId
		conditionVO.setBuildingId(objects[1]==null?null:new Long(objects[1].toString()));
		//buildingName
		conditionVO.setBuildingName(objects[2]==null?null:objects[2].toString());
		//houseId
		conditionVO.setHouseId(objects[3]==null?null:new Long(objects[3].toString()));
		//houseNumber
		conditionVO.setHouseNumber(objects[4]==null?null:objects[4].toString());
		//entrustMan
		conditionVO.setEntrustMan(objects[5]==null?null:objects[5].toString());
		//entrustItem
		conditionVO.setEntrustItem(objects[6]==null?null:objects[6].toString());
		//accepterId
		conditionVO.setAccepterId(objects[7]==null?null:new Long(objects[7].toString()));
		//accepterName
		conditionVO.setAccepterName(objects[8]==null?null:objects[8].toString());
		//acceptDate
		try
		{
			conditionVO.setAcceptDate(objects[9]==null?null:DateUtil.parseStrToDate(objects[9].toString()));
		}
		catch(ParseException e)
		{
			e.printStackTrace();
		}
		//entrustReason
		conditionVO.setEntrustReason(objects[10]==null?null:objects[10].toString());
		//enableState
		conditionVO.setEnableState(objects[11]==null?null:objects[11].toString());
		//givebackMan
		conditionVO.setGivebackMan(objects[12]==null?null:objects[12].toString());
		//givebackDate
		try
		{
			conditionVO.setGivebackDate(objects[13]==null?null:DateUtil.parseStrToDate(objects[13].toString()));
		}
		catch(ParseException e)
		{
			e.printStackTrace();
		}
		//givebackDescription
		conditionVO.setGivebackDescription(objects[14]==null?null:objects[14].toString());
		return conditionVO;
	}
}
